package wendu.jsbdemo;

import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.view.View;


import com.tencent.smtt.export.external.interfaces.WebResourceRequest;
import com.tencent.smtt.sdk.WebView;
import com.tencent.smtt.sdk.WebViewClient;

import wendu.dsbridge.DWebView;

public class JavascriptCallNativeActivity extends AppCompatActivity {

    private WebViewClient client = new WebViewClient() {

        @Override
        public void doUpdateVisitedHistory(WebView webView, String s, boolean b) {
//            LogUtils.e(s,b);

            if (!"about:blank".equals(s)) {
                super.doUpdateVisitedHistory(webView, s, b);
            }

        }

        /**
         * 防止加载网页时调起系统浏览器
         */
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return super.shouldOverrideUrlLoading(view, url);
        }


        public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
            view.loadUrl(request.getUrl().toString());
            return super.shouldOverrideUrlLoading(view, request);
        }


        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
        }


    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_js_call_native);
        final DWebView dwebView= (DWebView) findViewById(R.id.webview);
        dwebView.setWebViewClient(client);

        // set debug mode
        DWebView.setWebContentsDebuggingEnabled(true);
//        dwebView.addJavascriptObject(new JsApi(), null);
//        dwebView.addJavascriptObject(new JsEchoApi(),"echo");
        dwebView.loadUrl("http://lingxi.lingjue.net/game/breath?ran=4");
    }
}
